import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('172.16.230.177', 8000))
server.listen(1)
print('服务端正在监听端口8000...')
conn, addr = server.accept()
print(f'连接来自: {addr}')
key = 0x55
while True:
    encrypted_data = conn.recv(1024)
    if not encrypted_data:
        break
    decrypted_data = bytes([byte ^ key for byte in encrypted_data])
    decrypted_message = decrypted_data.decode('utf-8')
    print(f'接收到的消息: {decrypted_message}')
    with open('received.txt', 'a', encoding='utf-8') as f:
        f.write(decrypted_message + '\n')
    msg = input('请输入要发送给客户端的消息: ')
    msg_bytes = msg.encode('utf-8')
    encrypted_msg = bytes([byte ^ key for byte in msg_bytes])
    conn.send(encrypted_msg)
conn.close()
server.close()